home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / docp.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  2KB  |  87 lines

  1. #include "kiss.h"
  2.  
  3. int docp (Stringstack s)
  4. {
  5.     CpFlags
  6.     fl = { 0, 0, 0, 0, 0 };
  7.     register int
  8.     ret = 0,
  9.     i,
  10.     opt;
  11.     struct stat
  12.     statbuf;
  13.  
  14.     while ( (opt = getopt (s.nstr, s.str, "vrpdh")) != -1 )
  15.     switch (opt)
  16.     {
  17.         case 'v':
  18.         fl.verbose = 1;
  19.         break;
  20.         case 'r':
  21.         fl.recursive = 1;
  22.         break;
  23.         case 'p':
  24.         fl.preserve = 1;
  25.         break;
  26.         case 'd':
  27.         fl.noderef = 1;
  28.         break;
  29.         case 'i':
  30.         fl.interactive = 1;
  31.         break;
  32.         case 'h':
  33.         default:
  34.         error ("Bad commandline.\n"
  35.                "Usage: %s [-vpdi]  sourcefile destfile\n"
  36.                "       %s [-vpdi]  sourcfile(s) destdir\n"
  37.                "       %s [-vpdri] sourcedir destdir\n"
  38.                "       %s -h\n"
  39.                "Where:\n"
  40.                "    -d: no-dereference: copy symlinks as symlinks\n"
  41.                "    -h: this message\n"                            
  42.                "    -i: interactive, ask before overwriting\n"
  43.                "    -p: preserve file timestamps\n"
  44.                "    -r: copy recursively sourcedir to destdir\n"
  45.                "    -v: verbose output, show what's going on\n"
  46.                , progname, progname, progname, progname);
  47.     }
  48.  
  49.     /* need at least 2 args */
  50.     if (s.nstr - optind < 2)
  51.     error ("need at least 2 arguments");
  52.  
  53.     /* for multiple files: destination must be directory */
  54.     if (s.nstr - optind > 2)
  55.     {
  56.     if (fl.recursive)
  57.         error ("recursive copying only applies from one dir to another");
  58.     if (stat (s.str [s.nstr - 1], &statbuf) ||
  59.         ! S_ISDIR (statbuf.st_mode)
  60.        )
  61.         error ("\"%s\" is not a directory\n"
  62.            "multiple files must be copied to an existing directory",
  63.            s.str [s.nstr - 1]);
  64.     for (i = optind; i < s.nstr - 1; i++)
  65.         ret += copyfiletodir (s.str [i], s.str [s.nstr - 1], fl);
  66.     return (ret);
  67.     }
  68.  
  69.     /* exactly two files */
  70.     if (! stat (s.str [s.nstr - 1], &statbuf) &&
  71.     S_ISDIR (statbuf.st_mode)
  72.        )
  73.     {
  74.     if (! stat (s.str [optind], &statbuf) &&
  75.         S_ISDIR (statbuf.st_mode)
  76.        )
  77.     {
  78.         if (! fl.recursive)
  79.         error ("dir to dir copying can only be with recursive flag");
  80.         return (copydirtodir (s.str [optind], s.str [s.nstr - 1], fl, 0));
  81.     }
  82.     return (copyfiletodir (s.str [optind], s.str [s.nstr - 1], fl));
  83.     }
  84.     return (copyfiletofile (s.str [optind], s.str [s.nstr - 1], fl));
  85.     
  86. }
  87.